﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using static CoreShop.Library.GlobalEnumVars;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodsoldoutServices : Repository<corecmsfoodsoldout>, IcorecmsfoodsoldoutServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodsoldout entity)
        {
            return base.Change<corecmsfoodsoldout>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodsoldout> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodsoldout entity)
        {
            return base.Change<corecmsfoodsoldout>().Update(a => new corecmsfoodsoldout()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                foodId = entity.foodId,
                foodSpecId = entity.foodSpecId,
                soldout = entity.soldout,
                soldoutType = entity.soldoutType,
                autoSoldout = entity.autoSoldout,
                soldoutTime = entity.soldoutTime,
                recoveryTime = entity.recoveryTime,
                recoveryQty = entity.recoveryQty,
                modifyTime = Convert.ToInt64(DateTime.Now)
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodsoldout> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodsoldout>().Update(a => new corecmsfoodsoldout()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    foodId = item.foodId,
                    foodSpecId = item.foodSpecId,
                    soldout = item.soldout,
                    soldoutType = item.soldoutType,
                    autoSoldout = item.autoSoldout,
                    soldoutTime = item.soldoutTime,
                    recoveryTime = item.recoveryTime,
                    recoveryQty = item.recoveryQty,
                    modifyTime = Convert.ToInt64(DateTime.Now)
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodsoldout>().Update(a => new corecmsfoodsoldout()
            {
                deleted = -1
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodsoldout>().Update(a => new corecmsfoodsoldout()
                {
                    deleted = -1
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodsoldout> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodsoldout> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodsoldout> lst = new List<corecmsfoodsoldout>();
            var where = Expressionable.Create<corecmsfoodsoldout>();
            where = where.And(a => a.deleted != -1);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.foodSpecId == Convert.ToInt64(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).ToList();
            }

            return lst;
        }
        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodsoldout> QueryFoodSoldouts()
        {
            var list = base.AsQueryable()
                    .LeftJoin<corecmsfood>((p, f) => p.foodId == f.id)
                    .LeftJoin<corecmsfoodspec>((p, f, s) => p.foodSpecId == s.id)
                    .Select((p, f, s) => new corecmsfoodsoldout
                    {
                        id = p.id,
                        foodSpecId = s.id,
                        foodId = p.foodId,
                        foodName = f.name,
                        foodNumber = f.number,
                        soldout = p.soldout,
                        recoveryQty = p.recoveryQty,
                        autoSoldout = p.autoSoldout,
                        autoSoldoutTitle = p.recoveryQty.ToString(),
                        specName = s.specName,
                        soldoutType = p.soldoutType,
                        recoveryTime = p.recoveryTime,
                    }).ToList();

            foreach (var item in list)
            {
                if (item.autoSoldout == (int)GlobalEnumVars.AutoSoldout.One)
                    item.autoSoldoutTitle = "-";
                else item.autoSoldoutTitle = CommFunc.FormatDecimal(item.recoveryQty);
            }

            return list;
        }

        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodsoldout> QueryFoodSoldoutByIds(List<long> foodIds)
        {
            var list = base.AsQueryable().Where(p => foodIds.Contains(p.foodId)).ToList();
            return list;
        }

        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public corecmsfoodsoldout QueryFoodSoldoutById(long specId)
        {
            var list = base.AsQueryable()
                    .LeftJoin<corecmsfoodspec>((p, s) => p.foodSpecId == s.id)
                            .LeftJoin<corecmsfood>((p, s, f) => p.foodId == f.id)
                    .Where((p, s, f) => p.foodSpecId == specId)
                    .Select((p, s, f) => new corecmsfoodsoldout
                    {
                        id = p.id,
                        foodSpecId = s.id,
                        foodId = p.foodId,
                        foodName = f.name,
                        foodNumber = f.number,
                        soldoutType = p.soldoutType,
                        autoSoldout = p.autoSoldout,
                        soldout = p.soldout,
                        recoveryQty = p.recoveryQty,
                    }).First();

            return list;
        }

        #endregion


        /// <summary>
        /// 重置估清数据
        /// </summary>
        /// <param name="soldoutType"></param>
        /// <param name="list"></param>
        public void RecoverySoldout(int soldoutType, List<corecmsfoodsoldout> list)
        {
            if (list == null)
                list = base.AsQueryable().Where(p => p.soldoutType == soldoutType).ToList();

            if (list.Count > 0)
            {
                var autoList_One = list.Where(p => p.autoSoldout == (int)AutoSoldout.One).ToList();
                var autoList_Repeat = list.Where(p => p.autoSoldout == (int)AutoSoldout.Repeat).ToList();

                if (soldoutType == (int)GlobalEnumVars.SoldoutType.Class)//按班次
                {
                    foreach (var item_repaet in autoList_Repeat)
                    {
                        item_repaet.soldout = item_repaet.recoveryQty;
                        item_repaet.recoveryTime = DateTime.Now;
                    }
                }

                if (soldoutType == (int)GlobalEnumVars.SoldoutType.Day)//按营业日
                {
                    autoList_One = autoList_One.Where(p => DateTime.Now > (p.recoveryTime == null ? Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString()) : p.recoveryTime)).ToList();
                    autoList_Repeat = autoList_Repeat.Where(p => DateTime.Now > (p.recoveryTime == null ? Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString()) : p.recoveryTime)).ToList();
                    foreach (var item_repaet in autoList_Repeat)
                    {
                        item_repaet.soldout = item_repaet.recoveryQty;
                        item_repaet.recoveryTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString());
                    }
                }

                if (autoList_One.Count > 0)
                    base.Delete(autoList_One);
                if (autoList_Repeat.Count > 0)
                    base.UpdateRange(autoList_Repeat);
            }


        }
    }
}
